#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
@author: caijj
@version: V1.0
@file: ip_tool.py
@date: 2022/04/02 0026
"""
import pymssql
import pymysql
import requests
from fake_useragent import UserAgent
from jsonpath import jsonpath

from common.globalvar import GlobalVar


class IpTool(object):

    def __init__(self, db_type='SQL_Server', database_name='JRZF_BASE'):
        if db_type == 'SQL_Server':
            db_info = {
                'server': '192.168.106.21:1433',
                'user': 'base',
                'password': 'base',
                'database': database_name,
                'charset': 'utf8'
            }
            self.conn = pymssql.connect(**db_info)
        elif db_type == 'My_SQL':
            db_info = {
                # 'host': ini.mysql_database_host,
                # 'port': int(ini.mysql_database_port),
                # 'user': ini.mysql_database_account,
                # 'password': ini.mysql_database_password,
                'database': database_name,
                'charset': 'utf8'
            }
            self.conn = pymysql.connect(**db_info)
        else:
            raise ValueError('传值错误')

    @staticmethod
    def agent():
        ua = UserAgent(verify_ssl=False)
        headers = {'User-Agent': ua.random,
                   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,'
                             'application/signed-exchange;v=b3;q=0.9'}
        return headers

    def get_ip(self, url='http://dps.kdlapi.com/api/getdps/?orderid=984922218983115&num=5&signature'
                         '=vapl5cfmlcamh96kq6xsd835x0j05qh3&pt=1&format=json&sep=1'):
        headers = self.agent()  # 构造请求头
        # proxies = {
        #     "http": "http://127.0.0.1:8888",
        #     "https": "http://127.0.0.1:8888"
        # }
        # res = requests.get(url, headers=headers, proxies=proxies, verify=False)
        res = requests.get(url, headers=headers)
        ip_list = jsonpath(res.json(), '$.data.proxy_list')[0]
        return ip_list


if __name__ == '__main__':
    IpTool().get_ip()
